package com.chuyi.demo.frame;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractButton;
import javax.swing.JComponent;

public class DisableListener implements ItemListener {
	  private JComponent target;
	  private boolean defaultState;

	  private static void addListener(AbstractButton source, JComponent target,
					  boolean defaultState) {
	    DisableListener enablerOrDisabler =
	      new DisableListener(target, defaultState);
	    source.addItemListener(enablerOrDisabler);
	  }

	  public static void addDisabler(AbstractButton source, JComponent target) {
	    addListener(source, target, true);
	  }

	  public static void addEnabler(AbstractButton source, JComponent target) {
	    addListener(source, target, false);
	  }

	  public DisableListener(JComponent target, boolean defaultState) {
	    this.target = target;
	    this.defaultState = defaultState;
	    if (!defaultState)
	      target.setEnabled(false);
	  }

	  public void itemStateChanged(ItemEvent event) {
	    if (event.getStateChange() == ItemEvent.SELECTED)
	      target.setEnabled(!defaultState);
	    else
	      target.setEnabled(defaultState);
	  }
	}
